package com.czl.module_service.adapter.asset

import android.os.Bundle
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemChoosedSortBinding
import com.czl.module_service.fragment.assetUse.UsageApplyAddFragment
import java.util.ArrayList

class UsageSortChoosedAdapter(private val fragment: UsageApplyAddFragment) :
    BaseQuickAdapter<SortBean, BaseDataBindingHolder<ItemChoosedSortBinding>>(
        R.layout.item_choosed_sort
    ) {

    var position : Int = 0

    var isDel : Boolean = false

    override fun convert(
        holder: BaseDataBindingHolder<ItemChoosedSortBinding>,
        item: SortBean,
    ) {
        item.isDel = isDel
        item.position = holder.layoutPosition
        holder.dataBinding?.run {
            data = item
            adapter = this@UsageSortChoosedAdapter
            executePendingBindings()
        }
    }

    val chooseSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
            position = it.position!!
            data.filter { e -> e.sortId != null }
            val sortIds : MutableList<Int> = arrayListOf()
            data.forEach { e ->
                if (e.sortId != null) {
                    sortIds.add(e.sortId!!)
                }
            }
            fragment.startContainerActivity(AppConstants.Router.Service.F_SERVICE_SORT_CHOOSE_LIST, Bundle().apply {
                putSerializable(AppConstants.BundleKey.STORE_HOUSE_KEY, null)
                putSerializable("sortIds", sortIds as ArrayList<Int>)
            })
        }
    })

    val delSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
//            remove(it)
            fragment.deleteList(it)
        }
    })
}